//
//  ViewController.m
//  09-UITableViewTest
//
//  Created by 曹曹 on 16/7/26.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

#define kTableViewResuse @"reuseIdentifier"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableVeiw;
@property (nonatomic, strong) NSMutableArray *nameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nameArray = [NSMutableArray arrayWithArray:@[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"]];
    self.tableVeiw = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [_tableVeiw registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewResuse];
    _tableVeiw.delegate = self;
    _tableVeiw.dataSource = self;
    [self.view addSubview:_tableVeiw];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark -
// 实现以下两个方法并返回UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert即可实现tableView多选功能
//- (void)setEditing:animated:
//- (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableVeiw setEditing:editing animated:animated];
}
#pragma mark - UITableView Delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    return UITableViewCellEditingStyleDelete;
}
// 修改删除按钮的标题
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return @"我叫你你敢答应吗";
    }
    return @"删除";
}
#warning This method is amazing!
// 定制滑动操作
// 返回nil时执行上边方法的设置
// 返回@[]时无法进行编辑删除
// 先添加的在后边
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // UITableViewRowActionStyleNormal红色
    // UITableViewRowActionStyleDefault灰色
    // 颜色都可以在block外边改
    UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"收藏");
    }];
    rowAction1.backgroundColor = [UIColor redColor];
    UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 防止源数据丢失, 拷贝一份进行操作
        NSString *name = [[self.nameArray objectAtIndex:indexPath.row] copy];
        [_nameArray removeObjectAtIndex:indexPath.row];
        [_nameArray insertObject:name atIndex:0];
        [tableView reloadData];
    }];
    rowAction2.backgroundColor = [UIColor orangeColor];
    UITableViewRowAction *rowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_nameArray removeObjectAtIndex:indexPath.row];
        [_tableVeiw deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    rowAction3.backgroundColor = [UIColor grayColor];
    return @[rowAction3, rowAction2, rowAction1];
}
#pragma mark - UITableView Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewResuse];
    cell.textLabel.text = [_nameArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
